package com.tianqu.tms.www.member.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.member.actionform.MemberInputActionForm;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
/**
 * 跳转到会员录入信息确认页Action
 * 
 * @author tianshifu
 *
 */
public class MemberCreationConfirmAction extends TmsAction {
	
	private Logger logger = Logger.getLogger(MemberCreationConfirmAction.class);
	
	private static final String TOCLEAR = "toClear";//清空填入内容 放入一个标识字段证明进入到确认页面了
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		//从form中获取页面传来的数据，打包抛到前台，并set到uc中
		MemberInputActionForm memForm = (MemberInputActionForm)form;
		
		//将form表单数据set到viewhelp中
		MemberInputViewHelper viewhelp = (MemberInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		
		//从viewhelp中获取用户名到数据库中比较，存在则打出错误提示信息
		//用户唯一性验证
		request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		User user = new User();
		if(user.findUserInfoByUserId(viewhelp.getUserId(),0)==false)
		{
			setErrorsToRequest(request, TmsConstants.IC001 ,null);
			return mapping.getInputForward();				
		}
		Member mem = Member.getInstance();
		Attn attn = Attn.getInstance();
		if(!StringUtil.isNullOrBlank(viewhelp.getAccessCardId())){
			int mflag = mem.findMemberByAccessCardId(viewhelp.getAccessCardId());
			if(mflag == 2){
				setErrorsToRequest(request, TmsConstants.IC003 ,null);
				return mapping.getInputForward();
			}
			int cardNoFlag = mem.findMemberByAccessCardNo(viewhelp.getAccessCardNo());
			int attnFlag = attn.findAttnByAccessCardNo(viewhelp.getAccessCardNo());
			if(cardNoFlag == 2 || attnFlag == 2){
				setErrorsToRequest(request, TmsConstants.IC004 ,null);
				return mapping.getInputForward();
			}
		}
		
		
		uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		uc.removeAttribute(TOCLEAR);
		uc.setAttribute(TOCLEAR, TmsConstants.ONE);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
